package com.application.lucky.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.application.mylibrary.utils.LogUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author hemeng
 */
public class JsonParserUtil {

    public static <T> List<T> getArray(String json, Class<T> cls) {
        List<T> list = null;
        try {
            list = JSON.parseArray(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public static <T> T getBean(String json, Class<T> cls) {
        T t = null;
        try {
            t = JSON.parseObject(json, cls);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public static <T> T getBean(String json, TypeReference<T> type) {
        T t = null;
        try {
            t = JSON.parseObject(json, type);
            LogUtils.e("" + (t==null));
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.e(e.getMessage());
        }

        return t;
    }

    public static JSONObject getObject(String json) {
        JSONObject obj = null;
        try {
            obj = JSON.parseObject(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static JSONArray getArray(String json) {
        JSONArray obj = null;
        try {
            obj = JSON.parseArray(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static int getIntValue(JSONObject obj, String key) {
        int result = 0;
        if (obj == null) {
            return result;
        }
        try {
            result = obj.getIntValue(key);
        } catch (Exception e) {
        }
        return result;
    }

    public static int getIntValue(JSONObject obj, String key, int def) {

        int result = def;
        if (obj == null) {
            return result;
        }
        try {
            result = obj.getIntValue(key);
        } catch (Exception e) {
        }
        return result;
    }

//    public static long getLongValue(JSONObject obj, String key) {
//        long result = 0;
//        if (obj == null) {
//            return result;
//        }
//        try {
//            result = obj.getIntValue(key);
//        } catch (Exception e) {
//        }
//        return result;
//    }


    public static String getJsonFromFile(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
